package org.oc.qaq.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.oc.qaq.annotation.Autofill;
import org.oc.qaq.entity.FileTag;
import org.oc.qaq.entity.Tag;
import org.oc.qaq.enumeration.OperationType;
import org.oc.qaq.vo.FileTagVO;
import org.oc.qaq.vo.TagVO;

import java.util.List;

@Mapper
public interface TagMapper {

    @Autofill(OperationType.INSERT)
    void insert(Tag tag);

    List<TagVO> findByFileId(String fileId);

    @Select("SELECT * FROM tag WHERE created_by <> 'System'")
    List<Tag> findAll();

    @Select("SELECT * FROM tag WHERE name=#{name}")
    Tag findByName(String name);

    @Insert("INSERT INTO file_tag (tag_id, file_id) VALUES (#{tagId}, #{fileId})")
    void insertFileTag(String fileId, Long tagId);

    void insertFileTags(List<FileTag> fileIds);

    @Delete("DELETE FROM file_tag WHERE file_id=#{fileId}")
    void deleteFileTagByFileId(String fileId);

    @Select("SELECT * FROM tag where id=#{id}")
    Tag findById(Long id);

    void update(Tag tag);

    List<FileTagVO> selectTagsByFileIds(List<String> fileIds);

    @Delete("DELETE FROM file_tag WHERE file_id=#{id} and tag_id=999")
    void removeDeleteTag(String id);

    @Select("SELECT * FROM file_tag WHERE tag_id=#{id}")
    List<FileTag> selectFileTagsByTagId(Long id);

    @Delete("DELETE FROM tag WHERE id=#{id}")
    void delete(Long id);

    List<Tag> findTagsByRoleIds(List<Long> roleIds);

    @Insert("INSERT INTO role_tag (tag_id, role_id) VALUES (#{id}, #{roleId})")
    void insertTagRole(Long id, Long roleId);

    @Delete("DELETE FROM role_tag WHERE tag_id = #{id}")
    void deleteRoleTag(Long id);
}
